package com.xpc.controller.vo;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpc.config.StingToDate;
import com.xpc.mapper.AffairInfoMapper;
import com.xpc.pojo.Affair;
import com.xpc.pojo.AffairContent;
import com.xpc.pojo.vo.AffairInfo;
import com.xpc.pojo.vo.AffairUrlInfo;
import com.xpc.pojo.vo.AffairUrlInfo2;
import com.xpc.pojo.vo.UrlList;
import com.xpc.service.IAccountService;
import com.xpc.service.IAffairContentService;
import com.xpc.service.IAffairService;
import com.xpc.service.IAffairUrlInfoService;
import com.xpc.service.impl.AffairServiceImpl;
import com.xpc.service.impl.AffairUrlInfoServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * Project：UniversityGradeAdministrationSystem
 * Date：2022/6/14
 * Time：15:10
 * Description：TODO
 *
 * @author 南北辰的键盘
 * @version 1.0
 */
@RestController
@RequestMapping("affairInfo")
public class AffairInfoController extends ApiController {
    @Autowired
    private AffairInfoMapper affairInfoMapper;

    @Autowired
    private IAffairUrlInfoService iAffairUrlInfoService;

    @Autowired
    private IAccountService iAccountService;

    @Autowired
    private IAffairService iAffairService;

    @Autowired
    private IAffairContentService iAffairContentService;

    @Autowired
    private AffairUrlInfoServiceImpl affairUrlInfoService;

    /**
     * 获取全部事务列表信息
     * @param page
     * @param affairUrlInfo
     * @return
     */
    @GetMapping("all")
    public R selectAll(Page<AffairUrlInfo> page, AffairUrlInfo affairUrlInfo){
        List<AffairInfo> affairInfoList = this.affairInfoMapper.selectAllAffairInfo();
        return success(affairInfoList);
    }

//    @GetMapping("like/{s}")
//    public R selectLike(@PathVariable String s){
//        s = "'%" + s + "%'";
//        List<AffairInfo> affairInfoList = this.affairInfoMapper.selectLis;
//        return success(affairInfoList);
//    }


    /**
     * 通过获取指定事务内容等信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R selectById(@PathVariable String id){
        List<UrlList> linkUrl = this.affairInfoMapper.selectLinkById(id);
        List<UrlList> fileUrl = this.affairInfoMapper.selectFileById(id);
        AffairUrlInfo affairUrlInfo = this.affairInfoMapper.selectAffairInfoById(id);
        affairUrlInfo.setFileList(fileUrl);
        affairUrlInfo.setLinkList(linkUrl);
        return success(affairUrlInfo);
    }


    /**
     * 获取已完成事务项列表
     * @return
     */
    @GetMapping("fulfill")
    public R selectFulfill(){
        return success(this.affairInfoMapper.selectAllFulfillAffairInfo());
    }

    /**
     * 获取未完成事务项列表
     * @return
     */
    @GetMapping("noFulfill")
    public R selectNoFulfill(){
        return success(this.affairInfoMapper.selectAllNoFulfillAffairInfo());
    }

    @GetMapping("inform")
    public R selectInform(){
        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();

        return success(this.affairInfoMapper.selectInform(String.valueOf(iAccountService.getById(principal).getUserId())));
    }

//    @GetMapping("NoFulfillById")
//    public R selectNoFulfillById(@PathVariable String id){
//        return success(this.affairInfoMapper.);
//    }

    /**
     * 新建事务表（需要权限）
     * TODO：新建事务表（需要权限）
     * @param affairUrlInfo2
     * @return
     */
    @PostMapping("post")
    public R insert(@RequestBody AffairUrlInfo2 affairUrlInfo2){
        AffairContent affairContent =null;

        affairContent.setTheme(affairUrlInfo2.getTheme());
        affairContent.setParticular(affairUrlInfo2.getParticular());
        affairContent.setId(iAffairContentService.count()+1);
        boolean b = iAffairContentService.saveOrUpdate(affairContent);
        if (!b){
            return success(b);
        }

        Affair affair = null;

        affair.setId(iAffairService.count()+1);

        affair.setACoId(affairContent.getId());
        affair.setAClId(Integer.valueOf(affairUrlInfo2.getAClId()));

        Subject subject = SecurityUtils.getSubject();
        String principal = (String) subject.getPrincipal();
        affair.setCreator(iAccountService.getById(principal).getUserId());

        Date data = new Date();
        affair.setCreateTime(data);
        affair.setStart(1);
        StingToDate stingToDate = null;
        affair.setStartTime(stingToDate.ymdhms(affairUrlInfo2.getStartTime()));
        affair.setEndTime(stingToDate.ymdhms(affairUrlInfo2.getEndTime()));

        affair.setUrl(affairUrlInfo2.getUrl());
        affair.setFUrl(affairUrlInfo2.getFUrl());

        return success(iAffairService.saveOrUpdate(affair));

    }


    @DeleteMapping
    public R delete(@RequestParam("id") String id){
        Integer fatherId = iAffairService.getById(id).getFather();
        Integer sonId = iAffairService.getById(id).getSon();

//        if (null == sonId){
//            return success(this.iAffairService.removeById(id) && this.iAffairContentService.removeById(id));
//        }else{
//            AffairInfoController affairInfoController = null;
//            affairInfoController.delete(String.valueOf(iAffairService.getById(sonId)));
//            return success(this.iAffairService.removeById(id) && this.iAffairContentService.removeById(id));
//        }

        return success(this.iAffairService.removeById(id) && this.iAffairContentService.removeById(id));

    }






}
